<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>album_list</title>
</head>
<body>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="../css/style.css">

<script src="/js/vue.js"></script>
<script src="/js/axios.js"></script>
<script src="/js/elementui.js"></script>
<script src="/js/util.js"></script>

<div id="app">
    <el-button @click="formVisible = true" size="mini" type="primary" size="small">添加图片</el-button>
    <div class="demo-image__lazy">
        <img v-for="url in imgUrlList" :src="url" :key="url">
    </div>
    <div class="add-form">
        <!--弹出窗口-->
        <el-dialog  title="编辑" :visible.sync="formVisible" >
            <el-form label-width="80px">
                <el-form-item label="图片">
                    <el-upload
                            class="avatar-uploader"
                            action="/upload/native.do"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="save()">保存</el-button>
                    <el-button @click="formVisible = false" >关闭</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</div>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                searchMap: {},
                albumList: [],
                pojo: {},
                imgUrlList: [],
                formVisible:false,
                imageUrl:'',
                url:null,
                status:'success'
            }
        },
        created() {
            this.fetchData(getQueryString('id'));
        },
        methods: {
            fetchData(id) {
                axios.get(`/album/findById.do?id=${id}`).then(response => {
                    this.pojo = response.data
                    this.albumList = JSON.parse(this.pojo.imageItems)
                    if (this.albumList.length > 0) {
                        for (let i = 0; i < this.albumList.length; i++) {
                            this.imgUrlList.push(this.albumList[i].url)
                        }
                    }
                })
            },
            //[{"url":"http://localhost:9101/img/1.jpg","uid":1548143143154,"status":"success"},
            //{"url":"http://localhost:9101/img/7.jpg","uid":1548143143155,"status":"success"}]
            save (){
                this.pojo.
                this.imgUrlList[this.imgUrlList.length].url=this.imageUrl;
                this.imgUrlList[this.imgUrlList.length].uid= null
                this.imgUrlList[this.imgUrlList.length].status='success'
                this.imageUrl.url=this.imageUrl
                this.imgUrlList.push(this.imageUrl);
                JSON.stringify(this.imgUrlList)
                this.pojo.imageItems= this.imgUrlList; //如页面有图片上传功能放开注释
                alert(this.pojo.imageItems)
                axios.post(`/album/${this.pojo.id==null?'add':'update'}.do`,this.pojo).then(response => {
                    this.fetchData (); //刷新列表
                    this.formVisible = false ;//关闭窗口
                });
            },

            handleAvatarSuccess(res, file) {
                this.imageUrl = file.response;
            },

            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        }
    })
</script>
</body>
</html>